package org.jeecgframework.core.util.http;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import jeecg.business.entity.site.SiteEntity;

import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.clean.HtmlCleanBaseUtil;
import org.jeecgframework.core.util.icp.IcpUtil;

public class HttpClientUtil {
	
	public static String getEncode(HtmlCleaner cleaner, TagNode headNode) {
		Object[] metas;
		try {
			metas = headNode.evaluateXPath("//meta");
			//<meta charset="utf-8" />
			if (metas != null) {
				String metastr = null;
				TagNode tag = null;
				for (Object obj : metas) {
					if (obj instanceof TagNode) {
						tag = (TagNode) obj;

						metastr = HtmlCleanBaseUtil.getAllContent(cleaner, tag);
						metastr = metastr.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "");
						// System.out.println("meta:"+metastr);
						// System.out.println("reslut:"+(metastr.contains("utf-8")
						// || metastr.contains("UTF-8")));
						if (metastr.contains("utf-8") || metastr.contains("UTF-8") || metastr.contains("utf8") || metastr.contains("utf8"))
							return "utf-8";
						if (metastr.contains("gbk") || metastr.contains("GBK"))
							return "gbk";
						if (metastr.contains("gb2312") || metastr.contains("GB2312"))
							return "gb2312";
					}

				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}
	
	public static Integer timeout = 15000;
	public static Integer proxy_timeout = 30000;

	@SuppressWarnings("deprecation")
	public static String getCookies(String url, String charset) {
		DefaultHttpClient client = new DefaultHttpClient();
		client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout); 
		client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
		HttpGet get = null;
		url = "http://" + IcpUtil.getSiteFromUrl(url);
		if(url.contains("../")){
			url = url.replace("../", "");
		}
		try {
			get = new HttpGet(url);
			get.addHeader(
					"User-Agent",
					"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E; InfoPath.2)");

			get.addHeader(
					"Accept",
					"application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
			get.addHeader("Connection", "Keep-Alive");
			get.addHeader("Accept-Language", "zh-cn");
			get.addHeader("Accept-Encoding", "gzip, deflate");
			HttpResponse resp5 = client.execute(get);
			return getCookies(resp5);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (get != null)
				get.releaseConnection();
			client.getConnectionManager().shutdown();
			client.close();
		}
		return null;
	}
	
	
	
	@SuppressWarnings("deprecation")
	public static String getCookiesValue(String url, String charset, String cookies , String ip , int port , String headName) {
		DefaultHttpClient httpclient = new DefaultHttpClient();
		httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, proxy_timeout); 
		httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, proxy_timeout);
        //此代理不保证你看到的时候还存活
        HttpHost proxy = new HttpHost(ip, port);
        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
        String basehref = url.replace("http://", "");
        String [] array = basehref.split("/",2);
        array[1] = "/"+array[1];
        //记得将网址拆成以下形式
        System.out.println(array[0] +"  ---------  "+array[1]);
        HttpHost targetHost = new HttpHost(array[0]);    //网站名前面不要加http:// 
        HttpGet httpget = new HttpGet(array[1]);
        try{
	        HttpResponse response = httpclient.execute(targetHost, httpget);
	    	Header[] header = response.getHeaders(headName);
	    	String allString = "";
	    	for (int i = 0; i < header.length; i++) {
				String value = header[i].getValue();
				// value = value.substring(0,value.indexOf(";"));
				allString += value + "; ";
			}
			return allString;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			httpget.releaseConnection();
			httpclient.getConnectionManager().shutdown();
			httpclient.close();
		}
		return null;
	}

	public static Map<String , String> getAllHeader(HttpResponse resp){
		Header[] header = resp.getAllHeaders();
		Map<String , String> hmap = new HashMap<String , String>();
		for (int i = 0; i < header.length; i++) {
			String value = header[i].getValue();
			String key = header[i].getName();
			if(hmap.containsKey(key)){
				hmap.put(key, value + "; "+hmap.get(key));
			}else{
				hmap.put(key, value);
			}
		}
		return hmap;
	}
	
	public static String getCookies(HttpResponse resp) {
		Header[] header = resp.getHeaders("Set-Cookie");
		String allString = "";
		for (int i = 0; i < header.length; i++) {
			String value = header[i].getValue();
			// value = value.substring(0,value.indexOf(";"));
			allString += value + "; ";
		}
		return allString;
	}
	
	@SuppressWarnings("deprecation")
	public static String postSourceFromUrl(String url, String charset , Map<String , String> headers , Map<String , String> params , String cookies){
		DefaultHttpClient client = new DefaultHttpClient();
		client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout); 
		client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
		HttpPost post = null;
		try {
			post = new HttpPost(url);
			post.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET4.0C; .NET4.0E)");
			post.addHeader("Accept", "*/*");
			post.addHeader("Connection", "Keep-Alive");
			post.addHeader("Accept-Encoding", "gzip, deflate");
			post.addHeader("Accept-Language", "zh-cn");
			if (StringUtil.isNotEmpty(cookies))
				post.addHeader("Cookie", cookies);
			
			if(params != null){
				Iterator<String> it = params.keySet().iterator();
				List<NameValuePair> paramlist = new ArrayList<NameValuePair>();
				while (it.hasNext()) {
					String key = (String) it.next();
					paramlist.add(new BasicNameValuePair(key , params.get(key)));
				}
				post.setEntity(new UrlEncodedFormEntity(paramlist, charset));
			}
			HttpResponse resp5 = client.execute(post);
			InputStream in = resp5.getEntity().getContent();
			String searchResultHtml = CommonFunc.uncompressjson(in, charset);
			return searchResultHtml;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (post != null)
				post.releaseConnection();
			client.getConnectionManager().shutdown();
			client.close();
		}
		return null;
	}
	
	@SuppressWarnings("deprecation")
	public static String getSourceFromUrl(DefaultHttpClient client , String url, String charset, String cookies) {
		client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout); 
		client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
		HttpGet get = null;
		try {
			get = new HttpGet(url);
			get.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET4.0C; .NET4.0E)");
			get.addHeader("Accept", "*/*");
			get.addHeader("Connection", "Keep-Alive");
			get.addHeader("Accept-Encoding", "gzip, deflate");
			get.addHeader("Accept-Language", "zh-cn");
			if (StringUtil.isNotEmpty(cookies))
				get.addHeader("Cookie", cookies);

			HttpResponse resp5 = client.execute(get);
			
			InputStream in = resp5.getEntity().getContent();
			String searchResultHtml = CommonFunc.uncompressjson(in, charset);
			// System.out.println(searchResultHtml);
			return searchResultHtml;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (get != null)
				get.releaseConnection();
			client.getConnectionManager().shutdown();
			client.close();
		}
		return null;
	}
	
	public static Map<String , String> cookiesMap = new ConcurrentHashMap<String , String>();
	
	@SuppressWarnings("deprecation")
	public static String getSourceFromUrl(String url, String charset, String cookies , String ip , int port) {
		DefaultHttpClient httpclient = new DefaultHttpClient();
		httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, proxy_timeout); 
		httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, proxy_timeout);
        //此代理不保证你看到的时候还存活
        HttpHost proxy = new HttpHost(ip, port);
        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
    	
        String basehref = url.replace("http://", "").replace("https://", "");
        String [] array = basehref.split("/",2);
        array[1] = "/"+array[1];
        //记得将网址拆成以下形式
        System.out.println(array[0] +"  ---------  "+array[1]);
        HttpHost targetHost = new HttpHost(array[0]);    //网站名前面不要加http:// 
        HttpGet httpget = new HttpGet(array[1]);
        if (StringUtil.isNotEmpty(cookies))
    		httpget.addHeader("Cookie", cookies);
        try{
	        HttpResponse response = httpclient.execute(targetHost, httpget);
	        if(cookies == null){
	        	cookiesMap.put(ip+":"+port, HttpClientUtil.getCookies(response));
	        }
			InputStream in = response.getEntity().getContent();
			String searchResultHtml = CommonFunc.uncompressjson(in, charset);
			return searchResultHtml;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			httpget.releaseConnection();
			httpclient.getConnectionManager().shutdown();
			httpclient.close();
		}
		return null;
	}
	
	@SuppressWarnings("deprecation")
	public static String getCookies4Weixin(String url, String charset, String cookies ) {
		HttpGet get = null;
		DefaultHttpClient httpclient = new DefaultHttpClient();
		try {
			httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, proxy_timeout); 
			httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, proxy_timeout);
	        //此代理不保证你看到的时候还存活
			get = new HttpGet(url);
			get.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET4.0C; .NET4.0E)");
			get.addHeader("Accept", "*/*");
			get.addHeader("Connection", "Keep-Alive");
			get.addHeader("Accept-Encoding", "gzip, deflate");
			get.addHeader("Accept-Language", "zh-cn");
			if (StringUtil.isNotEmpty(cookies))
				get.addHeader("Cookie", cookies);

			HttpResponse resp5 = httpclient.execute(get);
			String allString = "";
			String  [] values = null;
			
//			Header[] headers = resp5.getHeaders("Set-Cookie");
//			for(Header h : headers){
//				values = h.getValue().split(";");
//				for(String value : values){
//					if(value.contains("ABTEST="))
//						allString += value+";";
//					if(value.contains("SUID="))
//						allString += value+";";
//					if(value.contains("SUV="))
//						allString += value+";";
//				}
//			}
			
			Header[] headers = resp5.getHeaders("x_log_ext");
			for(Header h : headers){
				values = h.getValue().toUpperCase().split("&");
				for(String value : values){
					if(value.toUpperCase().contains("SNUID="))
						allString += value.toUpperCase()+";";
				}
			}
			
			return allString;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (get != null)
				get.releaseConnection();
			httpclient.getConnectionManager().shutdown();
			httpclient.close();
		}
		return null;
	}
	
	@SuppressWarnings("deprecation")
	public static String getCookies4Weixin(String url, String charset, String cookies , String ip , int port) {
		
		HttpGet httpget = null;
		DefaultHttpClient httpclient = new DefaultHttpClient();
		try {
			
			httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, proxy_timeout); 
			httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, proxy_timeout);
	        //此代理不保证你看到的时候还存活
	        HttpHost proxy = new HttpHost(ip, port);
	        httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
	        String basehref = url.replace("http://", "");
	        String [] array = basehref.split("/",2);
	        array[1] = "/"+array[1];
	        //记得将网址拆成以下形式
	        System.out.println(array[0] +"  ---------  "+array[1]);
	        HttpHost targetHost = new HttpHost(array[0]);    //网站名前面不要加http:// 
	        httpget = new HttpGet(array[1]);

	        HttpResponse resp5 = httpclient.execute(targetHost, httpget);
			String allString = "";
			String  [] values = null;
			
			Header[] headers = resp5.getHeaders("Set-Cookie");
			for(Header h : headers){
				values = h.getValue().split(";");
				for(String value : values){
					if(value.contains("ABTEST="))
						allString += value+";";
					if(value.contains("SUID="))
						allString += value+";";
					if(value.contains("SUV="))
						allString += value+";";
				}
			}
			
			headers = resp5.getHeaders("x_log_ext");
			for(Header h : headers){
				values = h.getValue().toUpperCase().split("&");
				for(String value : values){
					if(value.toUpperCase().contains("SNUID="))
						allString += value.toUpperCase()+";";
					if(value.toUpperCase().contains("SUV="))
						allString += value.toUpperCase()+";";
				}
			}
			
			return allString;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (httpget != null)
				httpget.releaseConnection();
			httpclient.getConnectionManager().shutdown();
			httpclient.close();
		}
		return null;
	}

	public static String weixin_cookies;
	
	@SuppressWarnings("deprecation")
	public static String getRediectUrl(String url, String charset, String cookies){
		DefaultHttpClient client = new DefaultHttpClient();
		client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout); 
		client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
		client.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, false); //不进行重定向
		HttpGet get = null;
		try {
			get = new HttpGet(url);
			get.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET4.0C; .NET4.0E)");
			
			get.addHeader("Accept", "*/*");
			get.addHeader("Connection", "Keep-Alive");
			get.addHeader("Accept-Encoding", "gzip, deflate");
			get.addHeader("Accept-Language", "zh-cn");
			if (StringUtil.isNotEmpty(cookies))
				get.addHeader("Cookie", cookies);

			HttpResponse resp5 = client.execute(get);
			String location = resp5.getHeaders("Location")[0].getValue();
			return location;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (get != null)
				get.releaseConnection();
			client.getConnectionManager().shutdown();
			client.close();
		}
		return null;
	}
	
	@SuppressWarnings("deprecation")
	public static String getSourceFromUrl(String url, String charset, String cookies) {
		DefaultHttpClient client = new DefaultHttpClient();
		client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout); 
		client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
//		client.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, false); //不进行重定向
		HttpGet get = null;
		InputStream in = null;
		try {
			get = new HttpGet(url);
			get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36");
			
			get.addHeader("Accept", "*/*");
			get.addHeader("Connection", "Keep-Alive");
			get.addHeader("Accept-Encoding", "gzip, deflate");
			get.addHeader("Accept-Language", "zh-cn");
			if (StringUtil.isNotEmpty(cookies))
				get.addHeader("Cookie", cookies);

			HttpResponse resp5 = client.execute(get);
			in = resp5.getEntity().getContent();
			String searchResultHtml = null;
			searchResultHtml = CommonFunc.uncompress(in, charset);
//			 System.out.println(searchResultHtml.length());
			return searchResultHtml;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(in != null)
					in.close();
				if(get != null)
					get.releaseConnection();
				client.getConnectionManager().shutdown();
				client.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return null;
	}
	
	static SiteEntity site;
	
	static{
		site = new SiteEntity();
		site.setWyzfj("GBK");
		site.setLbtq("//div[@class='results']//div[@class='img_box2']//a[@href]");
		site.setLbxyy("//a[@id='sogou_next']");
		site.setZwbt("//title");
		site.setZwnr("//div[@id='js_content']");
		site.setZwsj("//em[@id='post-date']");
		site.setZwzz("//a[@id='post-user']");
	}

	public static void main(String[] args) {
//		System.out.println(new Date().getTime());
		//1445567806
		//1445568522986
		//http://www.darongcheng.com/Article/rcnews/20134/17372.html
		//http://www.rcmxw.com/mxview.asp?id=62967
		String url = "https://www.phil.pku.edu.cn/news.php";
//		String url = "http://www.sina.com.cn";
		String encode = "gb2312";
		String cookies = null;
		String ip = "181.188.194.177";
		int port = 80;
		String html = null;
		for(int i=0;i<100;i++){
			System.out.println("第"+(i+1)+"次");
			html = getSourceFromUrl(url , encode , cookies);
		}
		System.out.println(html);
		System.out.println(html.length());
		try {
			Thread.sleep(30000*1000L);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//181.188.194.177:80
		
//		DefaultHttpClient httpClient = new DefaultHttpClient();
//		String proxyHost = "117.177.243.111";
//		int proxyPort = 83;
//		
//		HttpParams params = new BasicHttpParams();
//		ConnManagerParams.setTimeout(params, 60 * 1000);
//		httpClient.setParams(params);
//		//设定目标站点  
//        
//		HttpHost proxy = new HttpHost(proxyHost, proxyPort, "http");
//        //对HttpClient对象设置代理  
//		httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
//	
//		
//		//String wxhref = "http://61.187.182.160:2001/feeling/";
//		String wxhref = "http://weixin.sogou.com/weixin";
//		
//		//String cookies = "CXID=6BAF35A8894F8D3EDE3F350A7AC8FEBD; SUID=6883FB24771C900A54FD177D0009B548; ssuid=9573797847; SUV=00D415097D58FFAF55027B71C4E25768; weixinIndexVisited=1; IPLOC=CN3505; ld=0vpkYkllll2qU8JYlllllVq0nOylllllrhHhwkllll1lllllpvoll5@@@@@@@@@@; usid=Wmc3aQKho0uClC2o; SNUID=4FB6C7558A8F9D231EDD5FAC8BE39189; ABTEST=6|1433387712|v1; ad=1kllllllll2U@SNdlllllVqhikclllllJ5mRzlllll9lllllxOxlw@@@@@@@@@@@; sct=98; wapsogou_qq_nickname=";
//		String cookies = null;
//		HttpGet get = null;
//		try {
//			get = new HttpGet(wxhref);
//			get.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET4.0C; .NET4.0E)");
//			get.addHeader("Accept", "*/*");
//			get.addHeader("Connection", "Keep-Alive");
//			get.addHeader("Accept-Encoding", "gzip, deflate");
//			get.addHeader("Accept-Language", "zh-cn");
//			if (StringUtil.isNotEmpty(cookies))
//				get.addHeader("Cookie", cookies);
//
//			HttpResponse resp5 = httpClient.execute(get);
//
//			InputStream in = resp5.getEntity().getContent();
//			String searchResultHtml = CommonFunc.uncompressjson(in, site.getWyzfj());
////			System.out.println(searchResultHtml);
//		} catch (Exception e) {
//			e.printStackTrace();
//		} finally {
//			if (get != null)
//				get.releaseConnection();
//		}
	}

}
